<?php

namespace App\Package\Finance;

use SplStack;
use Exception;
use Illuminate\Support\Facades\Log;
use App\Package\Finance\Contracts\StackContract;
use App\Package\Finance\Contracts\ElementContract;

/**
 * 表达式栈
 *
 * Date: 2018/6/6
 * @author George
 * @package App\Package\Finance
 */
class Stack implements StackContract
{
	/**
	 * 定义存储表达式元素的栈
	 *
	 * @var $stack
	 * Date: 2018/6/6
	 * @author George
	 */
	protected $stack;

	/**
	 * Stack constructor.
	 * @param SplStack $stack
	 */
	public function __construct(SplStack $stack)
	{
		$this->stack = $stack;
	}

	/**
	 * 入栈操作
	 *
	 * Date: 2018/6/6
	 * @author George
	 * @param ElementContract $element
	 * @return bool
	 */
	public function push(ElementContract $element): bool
	{
		try {
			$this->stack->push($element);
			return true;
		} catch (Exception $exception) {
			Log::error($exception);
			return false;
		}
	}

	/**
	 * 出栈操作
	 *
	 * Date: 2018/6/6
	 * @author George
	 * @return ElementContract
	 */
	public function pop(): ElementContract
	{
		return $this->stack->pop();
	}

}
